function varargout = MEMC_ImageWnd(varargin)
% MEMC_IMAGEWND M-file for MEMC_ImageWnd.fig
%      MEMC_IMAGEWND, by itself, creates a new MEMC_IMAGEWND or raises the existing
%      singleton*.
%
%      H = MEMC_IMAGEWND returns the handle to a new MEMC_IMAGEWND or the handle to
%      the existing singleton*.
%
%      MEMC_IMAGEWND('CALLBACK',hObject,eventData,handles,...) calls the local
%      function named CALLBACK in MEMC_IMAGEWND.M with the given input arguments.
%
%      MEMC_IMAGEWND('Property','Value',...) creates a new MEMC_IMAGEWND or raises the
%      existing singleton*.  Starting from the left, property value pairs are
%      applied to the GUI before MEMC_ImageWnd_OpeningFcn gets called.  An
%      unrecognized property name or invalid value makes property application
%      stop.  All inputs are passed to MEMC_ImageWnd_OpeningFcn via varargin.
%
%      *See GUI Options on GUIDE's Tools menu.  Choose "GUI allows only one
%      instance to run (singleton)".
%
% See also: GUIDE, GUIDATA, GUIHANDLES

% Edit the above text to modify the response to help MEMC_ImageWnd

% Last Modified by GUIDE v2.5 20-Jul-2010 16:55:58

% Begin initialization code - DO NOT EDIT
gui_Singleton = 0;
gui_State = struct('gui_Name',       mfilename, ...
                   'gui_Singleton',  gui_Singleton, ...
                   'gui_OpeningFcn', @MEMC_ImageWnd_OpeningFcn, ...
                   'gui_OutputFcn',  @MEMC_ImageWnd_OutputFcn, ...
                   'gui_LayoutFcn',  [] , ...
                   'gui_Callback',   []);
if nargin && ischar(varargin{1})
    gui_State.gui_Callback = str2func(varargin{1});
end

if nargout
    [varargout{1:nargout}] = gui_mainfcn(gui_State, varargin{:});
else
    gui_mainfcn(gui_State, varargin{:});
end
% End initialization code - DO NOT EDIT


% --- Executes just before MEMC_ImageWnd is made visible.
function MEMC_ImageWnd_OpeningFcn(hObject, eventdata, handles, varargin)
% This function has no output args, see OutputFcn.
% hObject    handle to figure
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)
% varargin   command line arguments to MEMC_ImageWnd (see VARARGIN)

% Choose default command line output for MEMC_ImageWnd
handles.output = hObject;
handles.IsMouseDown = 0;
handles.IsRightMouseDown = 0;
handles.PicPosCur = [0,0];
tmpPos  = get(handles.tagImageFig,'Position');
handles.MinWndSize  = tmpPos;
handles.PreWndSize  = handles.MinWndSize;
handles.Resized = 0;


set(hObject,'Resize','on');  %TBD ReSize

% Update handles structure
guidata(hObject, handles);

% UIWAIT makes MEMC_ImageWnd wait for user response (see UIRESUME)
% uiwait(handles.tagImageFig);


% --- Outputs from this function are returned to the command line.
function varargout = MEMC_ImageWnd_OutputFcn(hObject, eventdata, handles) 
% varargout  cell array for returning output args (see VARARGOUT);
% hObject    handle to figure
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)

% Get default command line output from handles structure
varargout{1} = handles.output;


% --- Executes on slider movement.
function slider1_Callback(hObject, eventdata, handles)
% hObject    handle to slider1 (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)

% Hints: get(hObject,'Value') returns position of slider
%        get(hObject,'Min') and get(hObject,'Max') to determine range of slider


% --- Executes during object creation, after setting all properties.
function slider1_CreateFcn(hObject, eventdata, handles)
% hObject    handle to slider1 (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    empty - handles not created until after all CreateFcns called

% Hint: slider controls usually have a light gray background.
if isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor'))
    set(hObject,'BackgroundColor',[.9 .9 .9]);
end


% --- Executes on slider movement.
function slider2_Callback(hObject, eventdata, handles)
% hObject    handle to slider2 (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)

% Hints: get(hObject,'Value') returns position of slider
%        get(hObject,'Min') and get(hObject,'Max') to determine range of slider


% --- Executes during object creation, after setting all properties.
function slider2_CreateFcn(hObject, eventdata, handles)
% hObject    handle to slider2 (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    empty - handles not created until after all CreateFcns called

% Hint: slider controls usually have a light gray background.
if isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor'))
    set(hObject,'BackgroundColor',[.9 .9 .9]);
end


% --- Executes on button press in tagImageZoomIn.
function tagImageZoomIn_Callback(hObject, eventdata, handles)
% hObject    handle to tagImageZoomIn (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)
ReCalImageShowParam(1,1,handles);
GetImageMBRectShow;
MEMC_MainWnd('UpdateAllImageWnd');

%hMEMC_MainWnd = findobj('Name','MEMC_MainWnd')
%hMEMC_MainWnd_DrawAllImage = findobj('String','MEMC_MainWnd_DrawALLImageWnd')
%setappdata(hMEMC_MainWnd,'TestData',23);
%MEMC_MAINWND('tagDrawImageWnd_ButtonDownFcn',hMEMC_MainWnd_DrawAllImage)

% --- Executes on button press in tagImageZoomOut.
function tagImageZoomOut_Callback(hObject, eventdata, handles)
% hObject    handle to tagImageZoomOut (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)
ReCalImageShowParam(1,0,handles);
GetImageMBRectShow;
MEMC_MainWnd('UpdateAllImageWnd');

% --- Executes during object creation, after setting all properties.
function tagImageAxes_CreateFcn(hObject, eventdata, handles)
% hObject    handle to tagImageAxes (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    empty - handles not created until after all CreateFcns called
% Hint: place code in OpeningFcn to populate tagImageAxes
set(hObject,'ActivePositionProperty', 'position');
tmpPos = get(hObject,'Position');
set(hObject, 'Position',[4,tmpPos(2),tmpPos(3),tmpPos(4)]);

%-----------------------------------------------------------
function DrawImageFunc(hObject,szNextPlot)
global GP;
handles = guidata(hObject);
WndIndex = find(GP.WndHdl==hObject);
set(handles.tagImageAxes,'NextPlot',szNextPlot);
imshow(GP.WndInfo(WndIndex).CDATA(GP.ImgParam.ZoomShownRect(2):GP.ImgParam.ZoomShownRect(4),GP.ImgParam.ZoomShownRect(1):GP.ImgParam.ZoomShownRect(3),:),...
       'Parent',handles.tagImageAxes);

function DrawMBRectFunc(hObject,bFirstDraw,szNextPlot)
global GP;
handles = guidata(hObject);  
set(handles.tagImageAxes,'NextPlot',szNextPlot);
if bFirstDraw ~= 1
   line(GP.CtrlParam.rectMBPre(1:5),GP.CtrlParam.rectMBPre(6:10),'Parent',handles.tagImageAxes,'EraseMode','xor');
end
line(GP.CtrlParam.rectMBCur(1:5),GP.CtrlParam.rectMBCur(6:10),'Parent',handles.tagImageAxes,'EraseMode','xor');

%----------------------------------------------------------------------
function ReCalImageShowParam(bIsZoomAction,bIsZoomIn,handles)
global GP;
minZoomScale = 0.25;
if bIsZoomAction == 1 && bIsZoomIn == 1
    GP.ImgParam.ZoomRatioCur = GP.ImgParam.ZoomRatioCur - minZoomScale;
    if GP.ImgParam.ZoomRatioCur<1
       GP.ImgParam.ZoomRatioCur = 1;
    end
elseif bIsZoomAction == 1
    if GP.ImgParam.ZoomRatioCur == 1
       GP.ImgParam.ZoomRatioCur = rem(GP.ImgParam.ZoomRatioIni,minZoomScale)+1;
       if GP.ImgParam.ZoomRatioCur == 1
           GP.ImgParam.ZoomRatioCur = GP.ImgParam.ZoomRatioCur + minZoomScale;
       end
    else
       GP.ImgParam.ZoomRatioCur = GP.ImgParam.ZoomRatioCur + minZoomScale;
    end
    if GP.ImgParam.ZoomRatioCur > GP.ImgParam.ZoomRatioIni
       GP.ImgParam.ZoomRatioCur = GP.ImgParam.ZoomRatioIni;
    end
end
%-------------
tmpSize = get(handles.tagImageAxes,'Position');  
NewShownSize = [round(GP.ImgParam.ZoomRatioCur*tmpSize(3)),round(GP.ImgParam.ZoomRatioCur*tmpSize(4))];
OldShownSize = [GP.ImgParam.ZoomShownRect(3)-GP.ImgParam.ZoomShownRect(1)+1,GP.ImgParam.ZoomShownRect(4)-GP.ImgParam.ZoomShownRect(2)+1];
AdjustOffset = floor((NewShownSize-OldShownSize)/2);

GP.ImgParam.ZoomShownRect(1) = GP.ImgParam.ZoomShownRect(1) - AdjustOffset(1); 
if GP.ImgParam.ZoomShownRect(1) < 1
   GP.ImgParam.ZoomShownRect(1) = 1;
end
GP.ImgParam.ZoomShownRect(3) = GP.ImgParam.ZoomShownRect(1) + NewShownSize(1)-1;
if GP.ImgParam.ZoomShownRect(3) > GP.ImgParam.Size(1)
   GP.ImgParam.ZoomShownRect(3) = GP.ImgParam.Size(1);
end
GP.ImgParam.ZoomShownRect(2) = GP.ImgParam.ZoomShownRect(2) - AdjustOffset(2); 
if GP.ImgParam.ZoomShownRect(2) < 1
   GP.ImgParam.ZoomShownRect(2) = 1;
end
GP.ImgParam.ZoomShownRect(4) = GP.ImgParam.ZoomShownRect(2) + NewShownSize(2)-1;
if GP.ImgParam.ZoomShownRect(4) > GP.ImgParam.Size(2)
   GP.ImgParam.ZoomShownRect(4) = GP.ImgParam.Size(2);
end

%----------------------------------------------------------
function [bIsOutside,PicPosCur] = GetImageWndPosition(handles)
global GP;
bIsOutside = 0;
pt = get(handles.tagImageAxes,'CurrentPoint');
pic_x = round(pt(1,1)) + GP.ImgParam.ZoomShownRect(1)-1;
pic_y = round(pt(1,2)) + GP.ImgParam.ZoomShownRect(2)-1;
if pic_x < GP.ImgParam.ZoomShownRect(1)
   pic_x = GP.ImgParam.ZoomShownRect(1);
   bIsOutside = 1;
elseif pic_x > GP.ImgParam.ZoomShownRect(3) 
   pic_x = GP.ImgParam.ZoomShownRect(3);
end
if pic_y < GP.ImgParam.ZoomShownRect(2)
   pic_y = GP.ImgParam.ZoomShownRect(2);
   bIsOutside = 1;
elseif pic_y > GP.ImgParam.ZoomShownRect(4) 
   pic_y = GP.ImgParam.ZoomShownRect(4);
   bIsOutside = 1;
end
PicPosCur = [pic_x,pic_y];

%----------------------------------------------------------------
function GetImageMBPosition(PixelPos)
global GP;
GP.CtrlParam.MBX = floor((PixelPos(1)-1)/8);
GP.CtrlParam.MBY = floor((PixelPos(2)-1)/8);
MEMC_MainWnd('UpdateMBPosText');


function GetImageMBRectShow
global GP;
tmpX = GP.CtrlParam.MBX*8 - GP.ImgParam.ZoomShownRect(1) + 1;
tmpY = GP.CtrlParam.MBY*8 - GP.ImgParam.ZoomShownRect(2) + 1;
TmpRectX =  [tmpX,tmpX+8,tmpX+8,tmpX,tmpX];
TmpRectY =  [tmpY,tmpY,tmpY+8,tmpY+8,tmpY];       
GP.CtrlParam.rectMBPre = GP.CtrlParam.rectMBCur;
GP.CtrlParam.rectMBCur = [TmpRectX,TmpRectY];

% --- Executes on mouse press over figure background, over a disabled or
% --- inactive control, or over an axes background.
function tagImageFig_WindowButtonDownFcn(hObject, eventdata, handles)
% hObject    handle to tagImageFig (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)
global GP;
handles.IsMouseDown = 1;
if strcmp(get(handles.tagImageFig,'SelectionType'),'alt')  %normal/alt/extend
    handles.IsRightMouseDown = 1;
else
    handles.IsRightMouseDown = 0;
end

[bIsOutside,PicPosCur] = GetImageWndPosition(handles);
if bIsOutside == 0
    set(handles.tagImageFig,'Pointer','fleur');
end
if GP.CtrlParam.IsMBTrace == 1 && bIsOutside == 0 && handles.IsRightMouseDown == 0
   GetImageMBPosition(PicPosCur);
   GetImageMBRectShow;
   MEMC_MainWnd('UpdateAllMBRectWnd',0);
end

if handles.IsRightMouseDown == 0
   MEMC_MainWnd('UpdatePicInfo',handles.tagImageFig,PicPosCur,bIsOutside);
end

handles.PicPosCur = PicPosCur;
guidata(handles.tagImageFig, handles);


% --- Executes on mouse motion over figure - except title and menu.
function tagImageFig_WindowButtonMotionFcn(hObject, eventdata, handles)
% hObject    handle to tagImageFig (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)
global GP;
[bIsOutside,PicPosCur] = GetImageWndPosition(handles);
if handles.IsMouseDown == 1 && handles.IsRightMouseDown == 1
   deltaPos = handles.PicPosCur - PicPosCur;
   if  ~isequal(deltaPos,zeros(1,2))
       tmpDelta = GP.ImgParam.ZoomShownRect(1) + deltaPos(1);
       if tmpDelta < 1
          deltaPos(1) = 1-GP.ImgParam.ZoomShownRect(1);
       end
       tmpDelta = GP.ImgParam.ZoomShownRect(3) + deltaPos(1);
       if tmpDelta > GP.ImgParam.Size(1)
          deltaPos(1) = GP.ImgParam.Size(1)-GP.ImgParam.ZoomShownRect(3);
       end
       tmpDelta = GP.ImgParam.ZoomShownRect(2) + deltaPos(2);
       if tmpDelta < 1
          deltaPos(2) = 1-GP.ImgParam.ZoomShownRect(2);
       end
       tmpDelta = GP.ImgParam.ZoomShownRect(4) + deltaPos(2);
       if tmpDelta > GP.ImgParam.Size(2)
          deltaPos(2) = GP.ImgParam.Size(2)-GP.ImgParam.ZoomShownRect(4);
       end
       GP.ImgParam.ZoomShownRect = GP.ImgParam.ZoomShownRect + [deltaPos,deltaPos];
       GetImageMBRectShow;
       MEMC_MainWnd('UpdateAllImageWnd');
       handles.PicPosCur = PicPosCur;
       guidata(handles.tagImageFig, handles);
   end
end
MEMC_MainWnd('UpdatePicPosInfo',guidata(GP.CtrlParam.hMainFigure),PicPosCur,bIsOutside);

    
% --- Executes on mouse press over figure background, over a disabled or
% --- inactive control, or over an axes background.
function tagImageFig_WindowButtonUpFcn(hObject, eventdata, handles)
% hObject    handle to tagImageFig (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)
handles.IsMouseDown = 0;
handles.IsRightMouseDown = 0;
set(handles.tagImageFig,'Pointer','arrow');
guidata(handles.tagImageFig, handles);

% --- Executes when tagImageFig is resized.
function tagImageFig_ResizeFcn(hObject, eventdata, handles)
% hObject    handle to tagImageFig (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)
global GP;
if GP.CtrlParam.ResizeDisable == 1
   return;
end
GP.CtrlParam.ResizeDisable = 1;

tmpPos  = get(handles.tagImageFig,'Position');
if tmpPos(3)<=handles.MinWndSize(3)
    tmpPos(3) = handles.MinWndSize(3);
end
if tmpPos(4)<=handles.MinWndSize(4)
    tmpPos(4) = handles.MinWndSize(4);
end
if (abs(tmpPos(3)-handles.PreWndSize(3))<2 || abs(tmpPos(4)-handles.PreWndSize(4))<2)
   set(handles.tagImageFig,'Position',handles.PreWndSize); 
   GP.CtrlParam.ResizeDisable = 0;
   return;
end
set(handles.tagImageFig,'Position',tmpPos);

deltaSize =  tmpPos(3:4)-handles.PreWndSize(3:4);
handles.PreWndSize = tmpPos;
tmpPos  = get(handles.tagImageAxes,'Position');
NewAxesSize = tmpPos(3:4)+deltaSize;
set(handles.tagImageAxes,'Position',[tmpPos(1:2),NewAxesSize]);

GP.ImgParam.ZoomRatioIni = max(GP.ImgParam.Size(1)/NewAxesSize(1),GP.ImgParam.Size(2)/NewAxesSize(2));
if GP.ImgParam.ZoomRatioCur > GP.ImgParam.ZoomRatioIni
    GP.ImgParam.ZoomRatioCur = GP.ImgParam.ZoomRatioIni;
end
ReCalImageShowParam(0,0,handles);
GetImageMBRectShow;
guidata(handles.tagImageFig, handles);
MEMC_MainWnd('UpdateAllImageWnd',handles.tagImageFig,deltaSize);
GP.CtrlParam.ResizeDisable = 0;

function ReSizeWndFunc(hObject,deltaSize)
handles = guidata(hObject);
tmpPos  = get(handles.tagImageFig,'Position');
handles.PreWndSize = [tmpPos(1:2),tmpPos(3:4) + deltaSize];
set(handles.tagImageFig,'Position',handles.PreWndSize);
tmpPos  = get(handles.tagImageAxes,'Position');
NewWndSize = [tmpPos(1),tmpPos(2),tmpPos(3:4)+deltaSize];
set(handles.tagImageAxes,'Position',NewWndSize);
guidata(handles.tagImageFig, handles);
   
% --- Executes when user attempts to close tagImageFig.
function tagImageFig_CloseRequestFcn(hObject, eventdata, handles)
% hObject    handle to tagImageFig (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)
% Hint: delete(hObject) closes the figure
hwndList = findobj('Name','MEMC_ImageInfo');
if ~isempty(hwndList);
  return;
end

MEMC_MainWnd('CloseImageOneWnd',hObject);
delete(hObject);


% --------------------------------------------------------------------
function tagShowInfo_Callback(hObject, eventdata, handles)
% hObject    handle to tagShowInfo (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)
global GP;
WndIndex = find(GP.WndHdl==handles.tagImageFig);
TypeAllSelected = {};
TypeAllScale = {};
TypeAllBlend = {};

for k = 1: length(GP.WndInfo(WndIndex).Type)
    tmpScrIndex = 1;
    while ~isequal(GP.SrcInfo(tmpScrIndex).Type,GP.WndInfo(WndIndex).Type{k}) || ~isequal(GP.SrcInfo(tmpScrIndex).szBaseName,char(GP.WndInfo(WndIndex).FileID{k}))
          tmpScrIndex = tmpScrIndex+1;
    end
    if isequal(GP.SrcInfo(tmpScrIndex).Type,{'AVI'})
     TypeAllSelected = {TypeAllSelected{:},{GP.SrcInfo(tmpScrIndex).szBaseName}};   
    else
      TypeAllSelected = {TypeAllSelected{:},GP.SrcInfo(tmpScrIndex).Type};
    end
    TypeAllScale    = {TypeAllScale{:},GP.SrcInfo(tmpScrIndex).Scale};
    TypeAllBlend    = {TypeAllBlend{:},GP.WndInfo(WndIndex).Blend{k}};
end

[TypeAllScale,TypeAllBlend] = MEMC_ImageInfo('MEMC_ImageTypeList',TypeAllSelected,TypeAllScale,TypeAllBlend);

for k = 1: length(GP.WndInfo(WndIndex).Type)
    tmpScrIndex = 1;
    while ~isequal(GP.SrcInfo(tmpScrIndex).Type,GP.WndInfo(WndIndex).Type{k}) || ~isequal(GP.SrcInfo(tmpScrIndex).szBaseName,char(GP.WndInfo(WndIndex).FileID{k}))
          tmpScrIndex = tmpScrIndex+1;
    end
    GP.SrcInfo(tmpScrIndex).Scale = TypeAllScale{k};
    GP.WndInfo(WndIndex).Blend{k} = TypeAllBlend{k};
end
MEMC_MainWnd('PlayUpdateNewImage');
MEMC_MainWnd('UpdateAllImageWnd');

